package com.example.hello1;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
      TextView output;
      private static final String Tag="开始运行";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bmi);
        Log.i(Tag, "onCreate:");

    }

    //匿名类创建方法
    public void myClick(View view) {

         Log.i(Tag,"Onclilck"); //日志设置
        //获取身高
        EditText edit_number=findViewById(R.id.shengao);
        String hei = edit_number.getText().toString();
        float hei_num = Float.parseFloat(hei);
        hei_num=hei_num/100;
        //获取体重
        EditText edit_name=findViewById(R.id.tizhong);
        String wei = edit_name.getText().toString();
        float wei_num = Float.parseFloat(wei);
        float bmi = wei_num/(hei_num * hei_num);
        bmi=(float)Math.round(bmi*100)/100;
        //显示用户数据
        TextView output=findViewById(R.id.out);
        output.setText("您的BMI指数为： "+bmi);
        output.setTextSize(24);
        //显示建议
        TextView output2= findViewById(R.id.bmi_sug);
        if(bmi<18.5)
        {
           String sug= "您的BMI指数偏低，请注意补充蛋白质";
            output2.setText(sug);
        }
        else if(bmi>=18.5&&bmi<=24.0)
        {
            String sug= "您的BMI指数正常，请继续保持";
            output2.setText(sug);
        }
        else if(bmi>24.0)
        {
            String sug= "您的BMI指数偏大，请注意加强锻炼，少吃油腻食物";
            output2.setText(sug);
        }




    }
    public void func(View view) {
        Log.i(Tag,"Onclilck"); //日志设置
        output = findViewById(R.id.out);  //局部变量和类变量同名时覆盖
        output.setText("请输入温度，并以C或者F结尾");
        //获取用户数据
        EditText edit_number=findViewById(R.id.input1);
        String number = edit_number.getText().toString();
        if(number.endsWith("C")){
            float fnum = Float.parseFloat(number.split("C")[0]);
            float ch_num = fnum * 9 / 5 + 32;
            TextView output = findViewById(R.id.out);
            output.setText("转换后的华氏温度为：" + ch_num);
        }
        else
        {
            float fnum = Float.parseFloat(number.split("F")[0]);
            float ch_num = (fnum -32)*5/9;
            TextView output = findViewById(R.id.out);
            output.setText("转换后的摄氏温度为：" + ch_num);
        }

    }
}